/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import dao.exceptions.IllegalOrphanException;
import dao.exceptions.NonexistentEntityException;
import dao.exceptions.RollbackFailureException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import pojos.JobsTitles;
import pojos.SourceCode;
import java.util.ArrayList;
import java.util.Collection;
import pojos.Employees;

/**
 *
 * @author A7med Kotb
 */
public class JobsTitlesJpaController implements Serializable {

    public JobsTitlesJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(JobsTitles jobsTitles) throws RollbackFailureException, Exception {
        if (jobsTitles.getSourceCodeCollection() == null) {
            jobsTitles.setSourceCodeCollection(new ArrayList<SourceCode>());
        }
        if (jobsTitles.getEmployeesCollection() == null) {
            jobsTitles.setEmployeesCollection(new ArrayList<Employees>());
        }
        if (jobsTitles.getEmployeesCollection1() == null) {
            jobsTitles.setEmployeesCollection1(new ArrayList<Employees>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Collection<SourceCode> attachedSourceCodeCollection = new ArrayList<SourceCode>();
            for (SourceCode sourceCodeCollectionSourceCodeToAttach : jobsTitles.getSourceCodeCollection()) {
                sourceCodeCollectionSourceCodeToAttach = em.getReference(sourceCodeCollectionSourceCodeToAttach.getClass(), sourceCodeCollectionSourceCodeToAttach.getScId());
                attachedSourceCodeCollection.add(sourceCodeCollectionSourceCodeToAttach);
            }
            jobsTitles.setSourceCodeCollection(attachedSourceCodeCollection);
            Collection<Employees> attachedEmployeesCollection = new ArrayList<Employees>();
            for (Employees employeesCollectionEmployeesToAttach : jobsTitles.getEmployeesCollection()) {
                employeesCollectionEmployeesToAttach = em.getReference(employeesCollectionEmployeesToAttach.getClass(), employeesCollectionEmployeesToAttach.getEmpId());
                attachedEmployeesCollection.add(employeesCollectionEmployeesToAttach);
            }
            jobsTitles.setEmployeesCollection(attachedEmployeesCollection);
            Collection<Employees> attachedEmployeesCollection1 = new ArrayList<Employees>();
            for (Employees employeesCollection1EmployeesToAttach : jobsTitles.getEmployeesCollection1()) {
                employeesCollection1EmployeesToAttach = em.getReference(employeesCollection1EmployeesToAttach.getClass(), employeesCollection1EmployeesToAttach.getEmpId());
                attachedEmployeesCollection1.add(employeesCollection1EmployeesToAttach);
            }
            jobsTitles.setEmployeesCollection1(attachedEmployeesCollection1);
            em.persist(jobsTitles);
            for (SourceCode sourceCodeCollectionSourceCode : jobsTitles.getSourceCodeCollection()) {
                JobsTitles oldRoleOfSourceCodeCollectionSourceCode = sourceCodeCollectionSourceCode.getRole();
                sourceCodeCollectionSourceCode.setRole(jobsTitles);
                sourceCodeCollectionSourceCode = em.merge(sourceCodeCollectionSourceCode);
                if (oldRoleOfSourceCodeCollectionSourceCode != null) {
                    oldRoleOfSourceCodeCollectionSourceCode.getSourceCodeCollection().remove(sourceCodeCollectionSourceCode);
                    oldRoleOfSourceCodeCollectionSourceCode = em.merge(oldRoleOfSourceCodeCollectionSourceCode);
                }
            }
            for (Employees employeesCollectionEmployees : jobsTitles.getEmployeesCollection()) {
                JobsTitles oldJobTitle2OfEmployeesCollectionEmployees = employeesCollectionEmployees.getJobTitle2();
                employeesCollectionEmployees.setJobTitle2(jobsTitles);
                employeesCollectionEmployees = em.merge(employeesCollectionEmployees);
                if (oldJobTitle2OfEmployeesCollectionEmployees != null) {
                    oldJobTitle2OfEmployeesCollectionEmployees.getEmployeesCollection().remove(employeesCollectionEmployees);
                    oldJobTitle2OfEmployeesCollectionEmployees = em.merge(oldJobTitle2OfEmployeesCollectionEmployees);
                }
            }
            for (Employees employeesCollection1Employees : jobsTitles.getEmployeesCollection1()) {
                JobsTitles oldJobTitle1OfEmployeesCollection1Employees = employeesCollection1Employees.getJobTitle1();
                employeesCollection1Employees.setJobTitle1(jobsTitles);
                employeesCollection1Employees = em.merge(employeesCollection1Employees);
                if (oldJobTitle1OfEmployeesCollection1Employees != null) {
                    oldJobTitle1OfEmployeesCollection1Employees.getEmployeesCollection1().remove(employeesCollection1Employees);
                    oldJobTitle1OfEmployeesCollection1Employees = em.merge(oldJobTitle1OfEmployeesCollection1Employees);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(JobsTitles jobsTitles) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            JobsTitles persistentJobsTitles = em.find(JobsTitles.class, jobsTitles.getJobTitleId());
            Collection<SourceCode> sourceCodeCollectionOld = persistentJobsTitles.getSourceCodeCollection();
            Collection<SourceCode> sourceCodeCollectionNew = jobsTitles.getSourceCodeCollection();
            Collection<Employees> employeesCollectionOld = persistentJobsTitles.getEmployeesCollection();
            Collection<Employees> employeesCollectionNew = jobsTitles.getEmployeesCollection();
            Collection<Employees> employeesCollection1Old = persistentJobsTitles.getEmployeesCollection1();
            Collection<Employees> employeesCollection1New = jobsTitles.getEmployeesCollection1();
            List<String> illegalOrphanMessages = null;
            for (SourceCode sourceCodeCollectionOldSourceCode : sourceCodeCollectionOld) {
                if (!sourceCodeCollectionNew.contains(sourceCodeCollectionOldSourceCode)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain SourceCode " + sourceCodeCollectionOldSourceCode + " since its role field is not nullable.");
                }
            }
            for (Employees employeesCollectionOldEmployees : employeesCollectionOld) {
                if (!employeesCollectionNew.contains(employeesCollectionOldEmployees)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Employees " + employeesCollectionOldEmployees + " since its jobTitle2 field is not nullable.");
                }
            }
            for (Employees employeesCollection1OldEmployees : employeesCollection1Old) {
                if (!employeesCollection1New.contains(employeesCollection1OldEmployees)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Employees " + employeesCollection1OldEmployees + " since its jobTitle1 field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<SourceCode> attachedSourceCodeCollectionNew = new ArrayList<SourceCode>();
            for (SourceCode sourceCodeCollectionNewSourceCodeToAttach : sourceCodeCollectionNew) {
                sourceCodeCollectionNewSourceCodeToAttach = em.getReference(sourceCodeCollectionNewSourceCodeToAttach.getClass(), sourceCodeCollectionNewSourceCodeToAttach.getScId());
                attachedSourceCodeCollectionNew.add(sourceCodeCollectionNewSourceCodeToAttach);
            }
            sourceCodeCollectionNew = attachedSourceCodeCollectionNew;
            jobsTitles.setSourceCodeCollection(sourceCodeCollectionNew);
            Collection<Employees> attachedEmployeesCollectionNew = new ArrayList<Employees>();
            for (Employees employeesCollectionNewEmployeesToAttach : employeesCollectionNew) {
                employeesCollectionNewEmployeesToAttach = em.getReference(employeesCollectionNewEmployeesToAttach.getClass(), employeesCollectionNewEmployeesToAttach.getEmpId());
                attachedEmployeesCollectionNew.add(employeesCollectionNewEmployeesToAttach);
            }
            employeesCollectionNew = attachedEmployeesCollectionNew;
            jobsTitles.setEmployeesCollection(employeesCollectionNew);
            Collection<Employees> attachedEmployeesCollection1New = new ArrayList<Employees>();
            for (Employees employeesCollection1NewEmployeesToAttach : employeesCollection1New) {
                employeesCollection1NewEmployeesToAttach = em.getReference(employeesCollection1NewEmployeesToAttach.getClass(), employeesCollection1NewEmployeesToAttach.getEmpId());
                attachedEmployeesCollection1New.add(employeesCollection1NewEmployeesToAttach);
            }
            employeesCollection1New = attachedEmployeesCollection1New;
            jobsTitles.setEmployeesCollection1(employeesCollection1New);
            jobsTitles = em.merge(jobsTitles);
            for (SourceCode sourceCodeCollectionNewSourceCode : sourceCodeCollectionNew) {
                if (!sourceCodeCollectionOld.contains(sourceCodeCollectionNewSourceCode)) {
                    JobsTitles oldRoleOfSourceCodeCollectionNewSourceCode = sourceCodeCollectionNewSourceCode.getRole();
                    sourceCodeCollectionNewSourceCode.setRole(jobsTitles);
                    sourceCodeCollectionNewSourceCode = em.merge(sourceCodeCollectionNewSourceCode);
                    if (oldRoleOfSourceCodeCollectionNewSourceCode != null && !oldRoleOfSourceCodeCollectionNewSourceCode.equals(jobsTitles)) {
                        oldRoleOfSourceCodeCollectionNewSourceCode.getSourceCodeCollection().remove(sourceCodeCollectionNewSourceCode);
                        oldRoleOfSourceCodeCollectionNewSourceCode = em.merge(oldRoleOfSourceCodeCollectionNewSourceCode);
                    }
                }
            }
            for (Employees employeesCollectionNewEmployees : employeesCollectionNew) {
                if (!employeesCollectionOld.contains(employeesCollectionNewEmployees)) {
                    JobsTitles oldJobTitle2OfEmployeesCollectionNewEmployees = employeesCollectionNewEmployees.getJobTitle2();
                    employeesCollectionNewEmployees.setJobTitle2(jobsTitles);
                    employeesCollectionNewEmployees = em.merge(employeesCollectionNewEmployees);
                    if (oldJobTitle2OfEmployeesCollectionNewEmployees != null && !oldJobTitle2OfEmployeesCollectionNewEmployees.equals(jobsTitles)) {
                        oldJobTitle2OfEmployeesCollectionNewEmployees.getEmployeesCollection().remove(employeesCollectionNewEmployees);
                        oldJobTitle2OfEmployeesCollectionNewEmployees = em.merge(oldJobTitle2OfEmployeesCollectionNewEmployees);
                    }
                }
            }
            for (Employees employeesCollection1NewEmployees : employeesCollection1New) {
                if (!employeesCollection1Old.contains(employeesCollection1NewEmployees)) {
                    JobsTitles oldJobTitle1OfEmployeesCollection1NewEmployees = employeesCollection1NewEmployees.getJobTitle1();
                    employeesCollection1NewEmployees.setJobTitle1(jobsTitles);
                    employeesCollection1NewEmployees = em.merge(employeesCollection1NewEmployees);
                    if (oldJobTitle1OfEmployeesCollection1NewEmployees != null && !oldJobTitle1OfEmployeesCollection1NewEmployees.equals(jobsTitles)) {
                        oldJobTitle1OfEmployeesCollection1NewEmployees.getEmployeesCollection1().remove(employeesCollection1NewEmployees);
                        oldJobTitle1OfEmployeesCollection1NewEmployees = em.merge(oldJobTitle1OfEmployeesCollection1NewEmployees);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = jobsTitles.getJobTitleId();
                if (findJobsTitles(id) == null) {
                    throw new NonexistentEntityException("The jobsTitles with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            JobsTitles jobsTitles;
            try {
                jobsTitles = em.getReference(JobsTitles.class, id);
                jobsTitles.getJobTitleId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The jobsTitles with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<SourceCode> sourceCodeCollectionOrphanCheck = jobsTitles.getSourceCodeCollection();
            for (SourceCode sourceCodeCollectionOrphanCheckSourceCode : sourceCodeCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This JobsTitles (" + jobsTitles + ") cannot be destroyed since the SourceCode " + sourceCodeCollectionOrphanCheckSourceCode + " in its sourceCodeCollection field has a non-nullable role field.");
            }
            Collection<Employees> employeesCollectionOrphanCheck = jobsTitles.getEmployeesCollection();
            for (Employees employeesCollectionOrphanCheckEmployees : employeesCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This JobsTitles (" + jobsTitles + ") cannot be destroyed since the Employees " + employeesCollectionOrphanCheckEmployees + " in its employeesCollection field has a non-nullable jobTitle2 field.");
            }
            Collection<Employees> employeesCollection1OrphanCheck = jobsTitles.getEmployeesCollection1();
            for (Employees employeesCollection1OrphanCheckEmployees : employeesCollection1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This JobsTitles (" + jobsTitles + ") cannot be destroyed since the Employees " + employeesCollection1OrphanCheckEmployees + " in its employeesCollection1 field has a non-nullable jobTitle1 field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(jobsTitles);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<JobsTitles> findJobsTitlesEntities() {
        return findJobsTitlesEntities(true, -1, -1);
    }

    public List<JobsTitles> findJobsTitlesEntities(int maxResults, int firstResult) {
        return findJobsTitlesEntities(false, maxResults, firstResult);
    }

    private List<JobsTitles> findJobsTitlesEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(JobsTitles.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public JobsTitles findJobsTitles(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(JobsTitles.class, id);
        } finally {
            em.close();
        }
    }

    public int getJobsTitlesCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<JobsTitles> rt = cq.from(JobsTitles.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
